package com.ruoyi.business.component;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import static com.ruoyi.business.service.ILoanApplyService.LOAN_APPLY_TOPIC;
import static com.ruoyi.business.service.ILoanApplyService.LOAN_RESULT_TOPIC;

@Slf4j
@Component
public class MqProduce {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 提现申请消息
     *
     * @param loanSerialNo
     * @param customerNo
     */
    public void sendLoanApplyOrderly(String loanSerialNo, String customerNo) {
        //risk返回后结束
        log.info("发送提现申请顺序消息，loanSerialNo：{},customerNo:{}", loanSerialNo, customerNo);
        SendResult sendResult = rocketMQTemplate.syncSendOrderly(LOAN_APPLY_TOPIC, loanSerialNo, customerNo);
        log.info("提现申请顺序消息发送结果，sendResult：{}", sendResult.toString());
    }

    /**
     * 放款回调结果消息
     *
     * @param loanSerialNo
     * @param result
     * @param customerNo
     */

    public void sendLoanResultOrderly(String loanSerialNo, String result, String customerNo) {
        //risk返回后结束
        log.info("放款回调结果消息推送MQ开始 >>> loanSerialNo：{}, result:{}, customerNo:{}", loanSerialNo, result, customerNo);
        SendResult sendResult = rocketMQTemplate.syncSendOrderly(LOAN_RESULT_TOPIC, buildLoanResultJson(loanSerialNo, result), customerNo);
        log.info("放款回调结果消息推送MQ结束 >>> sendResult：{}", sendResult.toString());
    }

    private static JSONObject buildLoanResultJson(String loanSerialNo, String result) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("loanSerialNo", loanSerialNo);
        jsonObject.put("result", result);
        return jsonObject;
    }

//    @PostConstruct
//    public void sendQueueMessage() {
//        //rocketMQTemplate.syncSendOrderly("repayment_topic_dev_jamnsin", "{\"custNo\":\"20220622176010035201\",\"msgObj\":{\"extMonnifyOrder\":{\"accountDetailsAccountName\":\"Monnify Limited\",\"accountDetailsAccountNumber\":\"0065432190\",\"accountDetailsAmountPaid\":\"1200\",\"accountDetailsBankCode\":\"\",\"amountPaid\":1200,\"cardDetails\":\"{}\",\"createTime\":1657110024768,\"currency\":\"NGN\",\"custNo\":\"20220622176010035201\",\"customerEmail\":\"a1585b0132@LCserver.com\",\"customerName\":\"baochao lili cheng\",\"eventType\":\"SUCCESSFUL_TRANSACTION\",\"id\":1431936,\"paidOn\":\"2022-07-06 12:20:023\",\"params\":{},\"paymentDescription\":\"bao\",\"paymentMethod\":\"ACCOUNT_TRANSFER\",\"paymentReference\":\"MNFY|21|20220706132023|000058\",\"paymentStatus\":\"PAID\",\"productReference\":\"20220623732271460354\",\"productType\":\"RESERVED_ACCOUNT\",\"settlementAmount\":1190.00,\"sourceData\":\"{\\\"product\\\":{\\\"reference\\\":\\\"20220623732271460354\\\",\\\"type\\\":\\\"RESERVED_ACCOUNT\\\"},\\\"transactionReference\\\":\\\"MNFY|21|20220706132023|000058\\\",\\\"paymentReference\\\":\\\"MNFY|21|20220706132023|000058\\\",\\\"eventType\\\":\\\"SUCCESSFUL_TRANSACTION\\\",\\\"paidOn\\\":\\\"2022-07-06 13:20:23.359\\\",\\\"paymentDescription\\\":\\\"bao\\\",\\\"metaData\\\":{},\\\"paymentSourceInformation\\\":[{\\\"bankCode\\\":\\\"\\\",\\\"amountPaid\\\":1200,\\\"accountName\\\":\\\"Monnify Limited\\\",\\\"sessionId\\\":\\\"sXeV4JHwsYpRNCJwO3RM4OVg94W3brdc\\\",\\\"accountNumber\\\":\\\"0065432190\\\"}],\\\"destinationAccountInformation\\\":{\\\"bankCode\\\":\\\"035\\\",\\\"bankName\\\":\\\"Wema bank\\\",\\\"accountNumber\\\":\\\"5000638384\\\"},\\\"amountPaid\\\":1200,\\\"totalPayable\\\":1200,\\\"cardDetails\\\":{},\\\"paymentMethod\\\":\\\"ACCOUNT_TRANSFER\\\",\\\"currency\\\":\\\"NGN\\\",\\\"settlementAmount\\\":\\\"1190.00\\\",\\\"paymentStatus\\\":\\\"PAID\\\",\\\"customer\\\":{\\\"name\\\":\\\"baochao lili cheng\\\",\\\"email\\\":\\\"a1585b0132@LCserver.com\\\"}}\",\"totalPayable\":1200,\"transactionReference\":\"MNFY|21|20220706132023|000058\",\"type\":\"TransferToUs\"},\"custNo\":\"20220622176010035201\"},\"type\":\"monnifyConsumer\"}", "98456232");
//        //sendLoanResultOrderly("20220707163250631939395586", "success", "2022070716325063193");
//        // rocketMQTemplate.syncSendOrderly(LOAN_RESULT_TOPIC, jsonObject, "98456233");
//    }

}